package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.pojo.KeChengClassify;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台课程类型
 * @author
 */
@ParentPackage("json-default")
@Action(value = "classify", results = {
        @Result(name = "classifyList", location = "/admin/classify/classifyList.jsp"),
        @Result(name = "save", type = "json",params = {"root","classify"}),
        @Result(name = "findSonClassify", type = "json",params = {"root","classifyList"})
})
@AllowedMethods(value = {("regex:.*")})
public class ClassifyAction extends BaseAction {
    @Autowired
    CommonDao dao;
    private KeChengClassify classify;
    private KeChengClassify classify_parent;
    private List<Integer> id;
    private List<KeChengClassify>classifyList;
    private String desc;//列表标题前缀

    /**
     * 分类列表
     * @return
     */
    public String classifyList(){
        //默认查找所有一级分类
        //classify表示当前集合的过滤条件
        Integer parent_level = 0;
        if(classify==null){
            classify = new KeChengClassify();
            classify.setParentId(0);
        }else {
            classify_parent = new KeChengClassify();
            classify_parent.setId(classify.getParentId());
            classify_parent = (KeChengClassify)dao.findOne(classify_parent);
            parent_level = classify_parent.getLevel()+1;
        }

        classifyList = (List<KeChengClassify>) dao.find(classify);

        switch (parent_level){
            case 0:
                desc = "一级";
                break;
            case 1:
                desc = "二级";
                break;
            case 2:
                desc = "三级";
                break;
        }
        return "classifyList";
    }

    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save(){
        if(classify.getId()!=null){
            KeChengClassify classify1 = new KeChengClassify();
            classify1.setId(classify.getId());
            classify1 = (KeChengClassify)dao.findOne(classify1);
            classify1.setSortedVal(classify.getSortedVal());
            classify1.setName(classify.getName());
            dao.update(classify1);
        }else{
            //判断是否添加的是一级分类，如果不是，则级别=上级+1
            if(classify.getParentId()==0){
                classify.setLevel(0);
            }else {
                KeChengClassify classify_p = new KeChengClassify();
                classify_p.setId(classify.getParentId());
                classify_p = (KeChengClassify)dao.findOne(classify_p);
                classify.setLevel(classify_p.getLevel()+1);
            }
            dao.add(classify);
        }
        return "save";
    }

    /**
     * 异步查询二级分类
     */
    public String findSonClassify(){
        classifyList = (List<KeChengClassify>) dao.find(classify);
        return "findSonClassify";
    }

    /**
     *删除
     * @return
     */
    @Transactional(readOnly = false)
    public String delete(){
        if(classify!=null)dao.delete(classify);
        else if(id!=null){
            for(Integer i:id){
                classify = new KeChengClassify();
                classify.setId(i);
                dao.delete(classify);
            }
        }
        return "redirectList";
    }


    public KeChengClassify getClassify() {
        return classify;
    }
    public void setClassify(KeChengClassify classify) {
        this.classify = classify;
    }
    public List<KeChengClassify> getClassifyList() {
        return classifyList;
    }
    public void setClassifyList(List<KeChengClassify> classifyList) {
        this.classifyList = classifyList;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public KeChengClassify getClassify_parent() {
        return classify_parent;
    }

    public void setClassify_parent(KeChengClassify classify_parent) {
        this.classify_parent = classify_parent;
    }
}
